package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.Drtypeinfo;
import com.ruoyi.system.service.IAppmanagerService;
import com.ruoyi.system.service.IDrtypeinfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RegGroup;
import com.ruoyi.system.service.IRegGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目变量分组Controller
 * 
 * @author yaozh
 * &#064;date  2024-02-29
 */
@RestController
@RequestMapping("/system/group")
public class RegGroupController extends BaseController
{
    @Resource
    private IRegGroupService regGroupService;
    @Resource
    private IAppmanagerService appManagerService;
    @Resource
    private IDrtypeinfoService drtypeinfoService;

    /**
     * 查询项目变量分组列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegGroup regGroup,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<RegGroup> list = regGroupService.selectRegGroupList(regGroup,dbName);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        List<RegGroup> list = regGroupService.selectRegGroupForAll(dbName);
        return success(list);
    }

    /**
     * 导出项目变量分组列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:export')")
    @Log(title = "项目变量分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegGroup regGroup,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<RegGroup> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = regGroupService.selectRegGroupList(regGroup,dbName);
        }
        ExcelUtil<RegGroup> util = new ExcelUtil<RegGroup>(RegGroup.class);
        util.exportExcel(response, list, "项目变量分组数据");
    }

    @Log(title = "项目变量分组", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:group:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<RegGroup> util = new ExcelUtil<RegGroup>(RegGroup.class);
        List<RegGroup> regGroupList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = regGroupService.importRegGroup(regGroupList, updateSupport, operName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<RegGroup> util = new ExcelUtil<RegGroup>(RegGroup.class);
        util.importTemplateExcel(response, "项目变量分组数据");
    }

    /**
     * 获取项目变量分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam String dbName)
    {
        return success(regGroupService.selectRegGroupById(id,dbName));
    }

    /**
     * 新增项目变量分组
     */
    @PreAuthorize("@ss.hasPermi('system:group:add')")
    @Log(title = "项目变量分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegGroup regGroup,@RequestParam String dbName)
    {
        return toAjax(regGroupService.insertRegGroup(regGroup,dbName));
    }

    /**
     * 修改项目变量分组
     */
    @PreAuthorize("@ss.hasPermi('system:group:edit')")
    @Log(title = "项目变量分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegGroup regGroup,@RequestParam String dbName)
    {
        return toAjax(regGroupService.updateRegGroup(regGroup,dbName));
    }

    /**
     * 删除项目变量分组
     */
    @PreAuthorize("@ss.hasPermi('system:group:remove')")
    @Log(title = "项目变量分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,@RequestParam String dbName)
    {
        return toAjax(regGroupService.deleteRegGroupByIds(ids,dbName));
    }

    /**
     * 查询设备类型管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/listDrTypeInfo")
    public AjaxResult listDrTypeInfo(Drtypeinfo drtypeinfo, @RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(drtypeinfoService.selectDrtypeinfoList(drtypeinfo,dbName));
    }

}
